import { scientificResearchCreate, scientificResearchUpdate } from "@/api/SecondmentDictionary";
import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { message } from "antd";
import { useState } from "react";
import { useTranslation } from "react-i18next";
let getScientificResearch: Function;
let sciId: string;
const timeFn = (time: string) => {
	if (!time) {
		return "";
	}
	const [date] = time.split(" ");
	return date;
};
const index = (props: { register: Fn }) => {
	const { t } = useTranslation();
	const formSchema: FormSchema[] = [
		{
			label: t("felix_ui.sq_numb"),
			field: "sciId", //科学研究id
			component: "Input"
		},
		{
			label: t("felix_ui.scientificResearchTopicName"),
			field: "sciResearchTopic", //科学研究课题名称
			component: "Input"
		},
		{
			label: t("felix_ui.grantTopic"),
			field: "topicLevel", //课题级别
			component: "Select",
			componentProps: {
				options: [
					{ label: t("secondmentDictionary.intemationalLevel"), value: "1" },
					{ label: t("secondmentDictionary.nationalLevel"), value: "2" },
					{ label: t("secondmentDictionary.ministryLevel"), value: "3" },
					{ label: t("secondmentDictionary.rovincial"), value: "4" },
					{ label: t("secondmentDictionary.agencyLevel"), value: "5" }
				]
			}
		},
		{
			label: t("felix_ui.unit"),
			field: "workplace", //单位
			component: "Input"
		},
		{
			label: t("felix_ui.dayApproved"),
			field: "dateApproval", //批准日期
			component: "DatePicker"
		},
		{
			label: t("felix_ui.fromDay"),
			field: "dateFrom", //开始日期
			component: "DatePicker"
		},
		{
			label: t("felix_ui.date_to"),
			field: "dateTo", //结束日期
			component: "DatePicker"
		},
		{
			label: t("felix_ui.performer_id"),
			field: "performer", //负责人
			component: "Input"
		},
		{
			label: t("felix_ui.engineeringGroup"),
			field: "workGroup", //团队
			component: "Input"
		}
		// {
		// 	label: t("felix_ui.unit"),
		// 	field: "workplace", //单位
		// 	component: "Input",
		// 	labelWidth: 200,
		// 	colProps: { span: 6 }
		// }
	];
	const [isAdd, setIsAdd] = useState(true);
	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		getScientificResearch = data.getScientificResearch;
		resetFields();
		if (data.record) {
			sciId = data.record.sciId;
			setFieldsValue(data.record);
			setIsAdd(false);
		}
	});

	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 180,
		baseColProps: {
			span: 24
		}
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		let api = scientificResearchCreate;
		if (!isAdd) {
			api = scientificResearchUpdate;
		}
		formValue.dateApproval = timeFn(formValue.dateApproval);
		formValue.dateFrom = timeFn(formValue.dateFrom);
		formValue.dateTo = timeFn(formValue.dateTo);
		const res = await api({ ...formValue, sciId });
		if (res) {
			sciId ? message.success(t("kevin_ui.addedSuccessfully")) : message.success(t("kevin_ui.updateSuccess"));
			getScientificResearch();
			closeDrawer();
		}
	};
	return (
		<BasicDrawer showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
